package com.cxh.rmsq.net.newNet

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.cxh.rmsq.db.model.AuthenticationBean
import com.cxh.rmsq.db.model.BaseResultListBean
import com.cxh.rmsq.db.model.CommunityContent
import com.cxh.rmsq.db.model.FriendShipInfo
import com.cxh.rmsq.db.model.GroupEntity
import com.cxh.rmsq.db.model.UnReadBean
import com.cxh.rmsq.db.model.UpdateBean
import com.cxh.rmsq.utils.toRequestBody
import kotlinx.coroutines.launch
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody
import java.io.File

class ProjectViewModel : ViewModel() {


    fun commentLiked(
        data: Any,
        func: (BaseResultEntity<Any>) -> Unit
    ) {
        viewModelScope.launch {
            func(
                ApiServiceClient.apiService.commentLiked(
                    toRequestBody(data)
                )
            )
        }
    }

    /**
     * 获取社区列表
     */
    fun getCommunityPage(
        userId: String,
        toUserId: String,
        pageNo: Int,
        pageSize: Int,
        func: (BaseResultEntity<BaseResultListBean>) -> Unit
    ) {
        viewModelScope.launch {
            func(
                ApiServiceClient.apiService.getCommunityPage(userId, toUserId, pageNo, pageSize)
            )
        }
    }

    /**
     * 获取订阅列表
     */
    fun getSubscriberCommunityPage(
        userId: String,
        pageNo: Int,
        pageSize: Int,
        func: (BaseResultEntity<BaseResultListBean>) -> Unit
    ) {
        viewModelScope.launch {
            func(
                ApiServiceClient.apiService.getSubscriberCommunityPage(userId, pageNo, pageSize)
            )
        }
    }

    /**
     * 获取好友动态列表
     */
    fun getFriendShipsCommunityPage(
        userId: String,
        pageNo: Int,
        pageSize: Int,
        func: (BaseResultEntity<BaseResultListBean>) -> Unit
    ) {
        viewModelScope.launch {
            func(
                ApiServiceClient.apiService.getFriendShipsCommunityPage(userId, pageNo, pageSize)
            )
        }
    }

    /**
     * 获得当前登录用户分组列表
     */
    fun getMyGroupList(func: (BaseResultEntity<MutableList<GroupEntity>>) -> Unit) {
        viewModelScope.launch {
            func(
                ApiServiceClient.apiService.getMyGroupList()
            )
        }
    }

    /**
     * 获得圈子列表
     */
    fun getCircleTypeList(func: (BaseResultEntity<MutableList<GroupEntity>>) -> Unit) {
        viewModelScope.launch {
            func(
                ApiServiceClient.apiService.getCircleTypeList()
            )
        }
    }

    /**
     * 获得圈子列表
     */
    fun getAuthentication(userId: String, func: (BaseResultEntity<AuthenticationBean>) -> Unit) {
        viewModelScope.launch {
            func(
                ApiServiceClient.apiService.getAuthentication(userId)
            )
        }
    }

    /**
     * 草稿发布
     */
    fun draftToPublish(
        data: Any,
        func: (BaseResultEntity<Any>) -> Unit
    ) {
        viewModelScope.launch {
            func(
                ApiServiceClient.apiService.draftToPublish(
                    toRequestBody(data)
                )
            )
        }
    }

    fun updateSinglePic(imagePath: String, func: (BaseResultEntity<String>) -> Unit) {
//        var body: MultipartBody.Part? = null
//        try {
//            val file = File(imagePath)
//            val requestFile =
//                RequestBody.create("multipart/form-data".toMediaTypeOrNull(), file)
//            body = MultipartBody.Part.createFormData(
//                "file",
//                file.name,
//                requestFile
//            ) //这个file名称很重要，后台怎么规定的这边怎么写
//        } catch (e: URISyntaxException) {
//            e.printStackTrace()
//        }

        val file1 = File(imagePath)
        val requestBody = RequestBody.create("multipart/form-data".toMediaTypeOrNull(), file1)
        val body = MultipartBody.Part.createFormData("file", file1.name, requestBody)

        viewModelScope.launch {
            func(ApiServiceClient.apiService.updateSinglePic(body))
        }
    }

    /**
     * 获取订阅列表
     */
    fun getSaveFollowerPage(
        type: Int,
        pageNo: Int,
        pageSize: Int,
        func: (BaseResultEntity<BaseResultListBean>) -> Unit
    ) {
        viewModelScope.launch {
            func(
                ApiServiceClient.apiService.getSaveFollowerPage(type, pageNo, pageSize)
            )
        }
    }

    /**
     * 谁看过我获得浏览用户历史分页
     */
    fun getUserBrowsingHistoryPage(
        type: Int,
        queryType: Int,
        pageNo: Int,
        pageSize: Int,
        func: (BaseResultEntity<BaseResultListBean>) -> Unit
    ) {
        viewModelScope.launch {
            func(
                ApiServiceClient.apiService.getUserBrowsingHistoryPage(
                    type,
                    queryType,
                    pageNo,
                    pageSize
                )
            )
        }
    }

    /**
     * 获得当前用户被点赞列表分页
     */
    fun selectCurrentLikedPage(
        data: Any,
        func: (BaseResultEntity<BaseResultListBean>) -> Unit
    ) {
        viewModelScope.launch {
            func(
                ApiServiceClient.apiService.selectCurrentLikedPage(
                    toRequestBody(data)
                )
            )
        }
    }

    /**
     * 获得当前用户被点赞列表分页
     */
    fun myCommentPage(
        /* type: Int,*/
        pageNo: Int,
        pageSize: Int,
        func: (BaseResultEntity<BaseResultListBean>) -> Unit
    ) {
        viewModelScope.launch {
            func(
                ApiServiceClient.apiService.myCommentPage(
                    /*type,*/ pageNo, pageSize
                )
            )
        }
    }

    /**
     * 删除用户社区订阅
     */
    fun createFollower(
        data: Any,
        func: (BaseResultEntity<Any>) -> Unit
    ) {
        viewModelScope.launch {
            func(
                ApiServiceClient.apiService.createFollower(
                    toRequestBody(data)
                )
            )
        }
    }

    /**
     * 获得个人社区动态
     */
    fun getselectSelfByPage(
        userId: String,
        toUserId: String,
        pageNo: Int,
        pageSize: Int,
        func: (BaseResultEntity<BaseResultListBean>) -> Unit
    ) {
        viewModelScope.launch {
            func(
                ApiServiceClient.apiService.getselectSelfByPage(userId, toUserId, pageNo, pageSize)
            )
        }
    }

    /**
     * 获得个人社区动态
     */
    fun getselectByPage(
        userId: String,
        pageNo: Int,
        pageSize: Int,
        func: (BaseResultEntity<BaseResultListBean>) -> Unit
    ) {
        viewModelScope.launch {
            func(
                ApiServiceClient.apiService.getselectByPage(userId, pageNo, pageSize)
            )
        }
    }

    /**
     * 获得个人草稿箱社区动态
     */
    fun getDraftPage(
        userId: String,
        pageNo: Int,
        pageSize: Int,
        func: (BaseResultEntity<BaseResultListBean>) -> Unit
    ) {
        viewModelScope.launch {
            func(
                ApiServiceClient.apiService.getDraftPage(userId, pageNo, pageSize)
            )
        }
    }

    /**
     * 获得草稿箱详情
     */
    fun getDraftContent(
        id: String,
        func: (BaseResultEntity<CommunityContent>) -> Unit
    ) {
        viewModelScope.launch {
            func(
                ApiServiceClient.apiService.getDraftContent(id)
            )
        }
    }

    /**
     * 草稿编辑
     */
    fun draftEditing(
        data: Any,
        func: (BaseResultEntity<Any>) -> Unit
    ) {
        viewModelScope.launch {
            func(
                ApiServiceClient.apiService.draftEditing(toRequestBody(data))
            )
        }
    }

    /**
     * App更新可见范围
     */
    fun appUpdateContent(
        data: Any,
        func: (BaseResultEntity<Any>) -> Unit
    ) {
        viewModelScope.launch {
            func(
                ApiServiceClient.apiService.appUpdateContent(toRequestBody(data))
            )
        }
    }

    /**
     * 是否置顶
     */
    fun topped(
        data: Any,
        func: (BaseResultEntity<Any>) -> Unit
    ) {
        viewModelScope.launch {
            func(
                ApiServiceClient.apiService.topped(toRequestBody(data))
            )
        }
    }

    /**
     * 是否置顶
     */
    fun deleteCommunity(
        id: String,
        func: (BaseResultEntity<Any>) -> Unit
    ) {
        viewModelScope.launch {
            func(
                ApiServiceClient.apiService.deleteCommunity(id)
            )
        }
    }

    /**
     * 是否置顶
     */
    fun getNoReadCount(
        func: (BaseResultEntity<UnReadBean>) -> Unit
    ) {
        viewModelScope.launch {
            func(
                ApiServiceClient.apiService.getNoReadCount()
            )
        }
    }

    /**
     * 是否置顶
     */
    fun createReportCommunity(
        data: Any,
        func: (BaseResultEntity<Any>) -> Unit
    ) {
        viewModelScope.launch {
            func(
                ApiServiceClient.apiService.createReportCommunity(toRequestBody(data))
            )
        }
    }

    /**
     * 是否置顶
     */
    fun getAppVersions(
        userId: String, platform: String, manufacturer: String,
        func: (BaseResultEntity<UpdateBean>) -> Unit
    ) {
        viewModelScope.launch {
            func(
                ApiServiceClient.apiService.getAppVersions(userId, platform, manufacturer)
            )
        }
    }

    /**
     * 注销
     */
    fun getLogoff(
        func: (BaseResultEntity<Any>) -> Unit
    ) {
        viewModelScope.launch {
            func(
                ApiServiceClient.apiService.getLogoff()
            )
        }
    }

    /**
     * 修改用户手机
     */
    fun updateMobile(
       data: Any, func: (BaseResultEntity<Any>) -> Unit
    ) {
        viewModelScope.launch {
            func(
                ApiServiceClient.apiService.updateMobile(toRequestBody(data))
            )
        }
    }


    /**
     * 获取所有好友信息
     */
    fun getAllFriendList(func: (BaseResultEntity<MutableList<FriendShipInfo>>) -> Unit
    ) {
        viewModelScope.launch {
            func(
                ApiServiceClient.apiService.getAllFriendList()
            )
        }
    }
}